home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 March
/
Chip_2002-03_cd1.bin
/
zkuste
/
delphi
/
kolekce
/
d3456
/
gmprintsuite_eval.exe
/
{app}
/
GmRtfPreview.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
2002-01-09
|
14KB
|
454 lines
{******************************************************************************}
{ }
{ TGmRtfPreview 2.3 }
{ }
{ Copyright (c) 2001 Graham Murt - www.MurtSoft.com }
{ }
{ Feel free to e-mail me with any comments, suggestions, bugs or help at: }
{ }
{ graham@murtsoft.com }
{ }
{******************************************************************************}
unit GmRtfPreview;
interface
{ To add RxRichEdit functionallity to TGmRtfPreview, simply
uncomment the line below...
}
// {$DEFINE RXRICHEDIT}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
GmPreview, ComCtrls, ClipBrd, StdCtrls
{$IFDEF RXRICHEDIT}
, RxRichEd
{$ENDIF}
;
type
TGmRtfPreview = class(TComponent)
private
FPreview: TGmPreview;
FTextFileFont: TFont;
procedure SetTextFileFont(AFont: TFont);
{ Private declarations }
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// procedure Load(ACustomMemo: TCustomMemo);
procedure LoadRichText(ACustomMemo: TCustomMemo);
//procedure LoadRtfFromFile(AFileName: string);
// procedure LoadRtfFromRichEdit(ARichEdit: TRichEdit; IsRichText: Boolean);
//procedure LoadRtfFromStream(AStream: TStream);
//procedure LoadRtfFromClipboard;
{$IFDEF RXRICHEDIT}
procedure LoadRtfFromRxFile(AFileName: string);
procedure LoadRtfFromRxRichEdit(ARichEdit: TCustomMemo; IsRichText: Boolean);
procedure LoadRtfFromRxStream(AStream: TStream);
{$ENDIF}
// procedure LoadTextFromFile(AFileName: string);
{ Public declarations }
published
property Preview: TGmPreview read FPreview write FPreview;
property TextFileFont: TFont read FTextFileFont write SetTextFileFont;
// events...
{ Published declarations }
end;
//procedure Register;
implementation
uses RichEdit;
constructor TGmRtfPreview.Create(AOwner: TComponent);
begin
inherited;
FTextFileFont := TFont.Create;
with FTextFileFont do
begin
Name := 'Arial';
Size := 12;
end;
end;
destructor TGmRtfPreview.Destroy;
begin
if Assigned(FTextFileFont) then FTextFileFont.Free;
inherited;
end;
{procedure TGmRtfPreview.Load(ACustomMemo: TCustomMemo);
var
IsRichText: Boolean;
begin
IsRichText := (ACustomMemo is TCustomRichEdit);
if ACustomMemo.ClassName = 'TRichEdit' then //LoadMemoText(ACustomMemo);
if ACustomMemo.ClassName = 'TRichEdit98' then //LoadMemoText(ACustomMemo, IsRichText);
//if ACustomMemo.ClassName = 'TRxRichEdit' then LoadRtfFromRxRichEdit(ACustomMemo, IsRichText);
end;}
procedure TGmRtfPreview.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FPreview) then
FPreview := nil;
end;
procedure TGmRtfPreview.SetTextFileFont(AFont: TFont);
begin
FTextFileFont.Assign(AFont);
end;
{procedure TGmRtfPreview.LoadTextFromFile(AFileName: string);
var
AParent: TForm;
ARichEdit: TRichEdit;
begin
AParent := TForm.Create(nil);
ARichEdit := TRichEdit.Create(AParent);
ARichEdit.Parent := AParent;
ARichEdit.Lines.LoadFromFile(AFileName);
Application.ProcessMessages;
ARichEdit.Font.Assign(FTextFileFont);
try
LoadRtfFromRichEdit(ARichEdit, False);
finally
ARichEdit.Free;
AParent.Free;
end;
end; }
procedure TGmRtfPreview.LoadRichText(ACustomMemo: TCustomMemo);
var
Range: TFormatRange;
LastChar, MaxLen, LogX, LogY, OldMap: Integer;
SaveRect: TRect;
mf: TMetafile;
mfc: TMetafileCanvas;
begin
if Assigned(FPreview) then with FPreview do
begin
{if not IsRichText then
begin
LastFont := TFont.Create;
LastFont.Assign(ACustomMemo.Font);
ACustomMemo.Font.Assign(FTextFileFont);
end
else
LastFont := nil; }
Clear;
mf := TMetafile.Create;
mfc := TMetafileCanvas.Create(mf, 0);
try
FillChar(Range, SizeOf(TFormatRange), 0);
with Range do
begin
hdc := mfc.Handle;
hdcTarget := hdc;
LogX := GetDeviceCaps(mfc.Handle, LOGPIXELSX);
LogY := GetDeviceCaps(mfc.Handle, LOGPIXELSY);
rc.Top := Round(((Margins.Top.AsInches+Header.Height.AsInches)+0.2) *96) * 1440 div LogY;
rc.Left := Round(Margins.Left.AsInches *96) * 1440 div LogX;
rc.bottom := Round((PageHeight.AsInches - ((Margins.Bottom.AsInches+Footer.Height.AsInches)+0.2))*96) * 1440 div LogX;
rc.right := Round((pageWidth.AsInches - Margins.Right.AsInches)*96) * 1440 div LogY;
rcPage := rc;
SaveRect := rc;
LastChar := 0;
MaxLen := ACustomMemo.GetTextLen;
chrg.cpMax := -1;
// ensure printer DC is in text map mode
OldMap := SetMapMode(hdc, MM_TEXT);
SendMessage(ACustomMemo.Handle, EM_FORMATRANGE, 0, 0); // flush buffer
try
repeat
rc := SaveRect;
chrg.cpMin := LastChar;
LastChar := SendMessage(ACustomMemo.Handle, EM_FORMATRANGE, 1, Longint(@Range));
if (LastChar < MaxLen) and (LastChar <> -1) then
begin
mfc.Free;
Canvas.Draw(0,0,mf,1,GmMillimeters);
NewPage;
mf.Clear;
mfc := TMetafileCanvas.Create(mf,0);
range.hdc := mfc.Handle;
range.hdcTarget := mfc.Handle;
end;
until (LastChar >= MaxLen) or (LastChar = -1);
finally
SendMessage(ACustomMemo.Handle, EM_FORMATRANGE, 0, 0); // flush buffer
SetMapMode(hdc, OldMap); // restore previous map mode
end;
end;
finally
mfc.Free;
Canvas.Draw(0,0,mf,1,GmMillimeters);
mf.Free;
{if not IsRichText then
begin
ARichEdit.Font.Assign(LastFont);
LastFont.Free;
end;}
end;
UpdatePreview;
end;
end;
{procedure TGmRtfPreview.LoadRtfFromFile(AFileName: string);
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(AFileName, fmOpenRead);
try
FileStream.Seek(0, soFromBeginning);
LoadRtfFromStream(FileStream);
finally
FileClose(FileStream.Handle);
end;
end;}
{procedure TGmRtfPreview.LoadRtfFromStream(AStream: TStream);
var
AParent: TForm;
ARichEdit: TRichEdit;
begin
AParent := TForm.Create(nil);
ARichEdit := TRichEdit.Create(AParent);
ARichEdit.Parent := AParent;
ARichEdit.Lines.LoadFromStream(AStream);
try
LoadRtfFromRichEdit(ARichEdit, True);
finally
ARichEdit.Free;
AParent.Free;
end;
end; }
{procedure TGmRtfPreview.LoadRtfFromClipboard;
var
AParent: TForm;
ARichEdit: TRichEdit;
begin
AParent := TForm.Create(nil);
ARichEdit := TRichEdit.Create(AParent);
ARichEdit.Parent := AParent;
ARichEdit.Text := (ClipBoard.AsText);
try
LoadRtfFromRichEdit(ARichEdit, True);
finally
ARichEdit.Free;
AParent.Free;
end;
end;}
{procedure TGmRtfPreview.LoadRtfFromRichEdit(ARichEdit: TRichEdit; IsRichText: Boolean);
var
Range: TFormatRange;
LastChar, MaxLen, LogX, LogY, OldMap: Integer;
SaveRect: TRect;
mf: TMetafile;
mfc: TMetafileCanvas;
LastFont: TFont;
TextLenEx: TGetTextLengthEx;
PercentDone: Extended;
begin
if Assigned(FPreview) then with FPreview do
begin
if not IsRichText then
begin
LastFont := TFont.Create;
LastFont.Assign(ARichEdit.Font);
ARichEdit.Font.Assign(FTextFileFont);
end
else
LastFont := nil;
Clear;
mf := TMetafile.Create;
mfc := TMetafileCanvas.Create(mf, 0);
try
FillChar(Range, SizeOf(TFormatRange), 0);
with Range do
begin
hdc := mfc.Handle;
hdcTarget := hdc;
LogX := GetDeviceCaps(mfc.Handle, LOGPIXELSX);
LogY := GetDeviceCaps(mfc.Handle, LOGPIXELSY);
rc.Top := Round((Margins.Top.AsInches+Header.Height.AsInches) *96) * 1440 div LogX;
rc.Left := Round(Margins.Left.AsInches *96) * 1440 div LogX;
rc.bottom := Round((PageHeight.AsInches - ((Margins.Bottom.AsInches+Footer.Height.AsInches)+0.2))*96) * 1440 div LogX;
rc.right := Round((pageWidth.AsInches - Margins.Right.AsInches)*96) * 1440 div LogY;
rcPage := rc;
SaveRect := rc;
LastChar := 0;
MaxLen := ARichEdit.GetTextLen;
chrg.cpMax := -1;
// ensure printer DC is in text map mode
OldMap := SetMapMode(hdc, MM_TEXT);
SendMessage(ARichEdit.Handle, EM_FORMATRANGE, 0, 0); // flush buffer
try
PercentDone := 0;
repeat
rc := SaveRect;
chrg.cpMin := LastChar;
LastChar := SendMessage(ARichEdit.Handle, EM_FORMATRANGE, 1, Longint(@Range));
if (LastChar < MaxLen) and (LastChar <> -1) then
begin
mfc.Free;
Canvas.Draw(0,0,mf,1,GmMillimeters);
NewPage;
mf.Clear;
mfc := TMetafileCanvas.Create(mf,0);
range.hdc := mfc.Handle;
range.hdcTarget := mfc.Handle;
end;
// PercentDone :=
until (LastChar >= MaxLen) or (LastChar = -1);
finally
SendMessage(ARichEdit.Handle, EM_FORMATRANGE, 0, 0); // flush buffer
SetMapMode(hdc, OldMap); // restore previous map mode
end;
end;
finally
mfc.Free;
Canvas.Draw(0,0,mf,1,GmMillimeters);
if not IsRichText then
begin
ARichEdit.Font.Assign(LastFont);
LastFont.Free;
end;
end;
UpdatePreview;
end;
end; }
{$IFDEF RXRICHEDIT}
procedure TGmRtfPreview.LoadRtfFromRxFile(AFileName: string);
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(AFileName, fmOpenRead);
try
FileStream.Seek(0, soFromBeginning);
LoadRtfFromRxStream(FileStream);
finally
FileClose(FileStream.Handle);
end;
end;
procedure TGmRtfPreview.LoadRtfFromRxRichEdit(ARichEdit: TCustomMemo; IsRichText: Boolean);
var
Range: TFormatRange;
LastChar, MaxLen, LogX, LogY, OldMap: Integer;
SaveRect: TRect;
TextLenEx: TGetTextLengthEx;
mf: TMetafile;
mfc: TMetafileCanvas;
LastFont: TFont;
begin
if Assigned(FPreview) then with FPreview do
begin
{if not IsRichText then
begin
LastFont := TFont.Create;
LastFont.Assign(ARichEdit.Font);
ARichEdit.Font.Assign(FTextFileFont);
end
else
LastFont := nil;
Clear;}
mf := TMetafile.Create;
mfc := TMetafileCanvas.Create(mf, 0);
try
FillChar(Range, SizeOf(TFormatRange), 0);
with Range do
begin
hdc := mfc.Handle;
hdcTarget := hdc;
LogX := GetDeviceCaps(mfc.Handle, LOGPIXELSX);
LogY := GetDeviceCaps(mfc.Handle, LOGPIXELSY);
rc.Top := Round((Margins.Top.AsInches+Header.Height.AsInches) *96) * 1440 div LogX;
rc.Left := Round(Margins.Left.AsInches *96) * 1440 div LogX;
rc.bottom := Round((PageHeight.AsInches - ((Margins.Bottom.AsInches+Footer.Height.AsInches)+0.2))*96) * 1440 div LogX;
rc.right := Round((pageWidth.AsInches - Margins.Right.AsInches)*96) * 1440 div LogY;
rcPage := rc;
SaveRect := rc;
LastChar := 0;
if RichEditVersion >= 2 then
begin
with TextLenEx do begin
flags := GTL_DEFAULT;
codepage := CP_ACP;
end;
MaxLen := ARichEdit.perform(EM_GETTEXTLENGTHEX, WParam(@TextLenEx), 0);
end
else MaxLen := GetTextLen;
chrg.cpMax := -1;
{ ensure printer DC is in text map mode }
OldMap := SetMapMode(hdc, MM_TEXT);
SendMessage(ARichEdit.Handle, EM_FORMATRANGE, 0, 0); { flush buffer }
try
repeat
rc := SaveRect;
chrg.cpMin := LastChar;
LastChar := SendMessage(ARichEdit.Handle, EM_FORMATRANGE, 1, Longint(@Range));
if (LastChar < MaxLen) and (LastChar <> -1) then
begin
mfc.Free;
Canvas.Draw(0,0,mf,1,GmMillimeters);
NewPage;
mf.Clear;
mfc := TMetafileCanvas.Create(mf,0);
range.hdc := mfc.Handle;
range.hdcTarget := mfc.Handle;
end;
until (LastChar >= MaxLen) or (LastChar = -1);
finally
SendMessage(ARichEdit.Handle, EM_FORMATRANGE, 0, 0); // flush buffer
SetMapMode(hdc, OldMap); // restore previous map mode
end;
end;
finally
mfc.Free;
Canvas.Draw(0,0,mf,1,GmMillimeters);
{ if not IsRichText then
begin
ARichEdit.Font.Assign(LastFont);
LastFont.Free;
end;}
end;
UpdatePreview;
end;
end;
procedure TGmRtfPreview.LoadRtfFromRxStream(AStream: TStream);
var
AParent: TForm;
ARichEdit: TRxRichEdit;
begin
AParent := TForm.Create(nil);
ARichEdit := TRxRichEdit.Create(AParent);
ARichEdit.Parent := AParent;
ARichEdit.Lines.LoadFromStream(AStream);
try
LoadRtfFromRxRichEdit(ARichEdit, True);
finally
ARichEdit.Free;
AParent.Free;
end;
end;
{$ENDIF}
end.